package com.xk857.service.impl;

import com.alibaba.fastjson.annotation.JSONField;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;


import java.util.List;

/**
 * @author CV大魔王
 * @Description 用户对象，SpringSecurity用于给用户赋予权限等信息
 * @date 2021/2/26 21:43
 */
@Data
public class JwtUser implements UserDetails {

    @ApiModelProperty(value = "用户ID")
    private String uid;

    @ApiModelProperty(value = "用户名")
    private String username;

    @JSONField(serialize = false) // 忽略转json
    @ApiModelProperty(value = "密码，加密存储")
    private String password;

    @ApiModelProperty(value = "昵称")
    private String nickName;

    @ApiModelProperty(value = "头像url")
    private String imageUrl;

    @ApiModelProperty(value = "注册手机号")
    private String mobile;

    @ApiModelProperty(value = "注册邮箱")
    private String email;

    // 1 true 0 false
    @JSONField(serialize = false)
    @ApiModelProperty(value = "帐户是否过期(1 未过期，0已过期)")
    private boolean isAccountNonExpired; // 不要写小写 boolean

    @JSONField(serialize = false)
    @ApiModelProperty(value = "帐户是否被锁定(1 未过期，0已过期)")
    private boolean isAccountNonLocked;

    @JSONField(serialize = false)
    @ApiModelProperty(value = "密码是否过期(1 未过期，0已过期)")
    private boolean isCredentialsNonExpired;

    @JSONField(serialize = false)
    @ApiModelProperty(value = "帐户是否可用(1 可用，0 删除用户)")
    private boolean isEnabled;

    /** 封装用户拥有的菜单权限标识 */
    @JSONField(serialize = false) // 忽略转json
    private List<GrantedAuthority> authorities;


    public JwtUser(String uid, String username, String password, String nickName, String imageUrl, String mobile, String email, Integer isAccountNonExpired, Integer isAccountNonLocked, Integer isCredentialsNonExpired, Integer isEnabled, List<GrantedAuthority> authorities) {
        this.uid = uid;
        this.username = username;
        this.password = password;
        this.nickName = nickName;
        this.imageUrl = imageUrl;
        this.mobile = mobile;
        this.email = email;
        this.isAccountNonExpired = isAccountNonExpired == 1; // 如果不好理解，这里转换成三元运算符方便大家理解 isAccountNonExpired == 1 ? true : false
        this.isAccountNonLocked = isAccountNonLocked == 1;
        this.isCredentialsNonExpired = isCredentialsNonExpired == 1;
        this.isEnabled = isEnabled == 1;
        this.authorities = authorities;
    }

}
